package pro.cherish.dao.redis.hellper;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import pro.cherish.knowledgeutil.str.StringUtilExt;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-11-02 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：BasicRedisTemplate
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-11-02 cherish
 */
@Component
public class BasicRedisTemplate implements IBasicRedisTemplate {

    /**
     * 聚合stringRedisTemplate
     */
    private StringRedisTemplate stringRedisTemplate;

    public BasicRedisTemplate(StringRedisTemplate stringRedisTemplate){
        this.stringRedisTemplate = stringRedisTemplate;
    }

//    /**
//     * 将key 的值设为value ，当且仅当key 不存在，等效于 SETNX。
//     */
//    private static final String NX = "NX";
//
//    /**
//     * 将key 的值设为value ，当且仅当key 存在，等效于 SETXX。
//     */
//    private static final String XX = "XX";
//
//    /**
//     * seconds — 以秒为单位设置 key 的过期时间，等效于EXPIRE key seconds
//     */
//    private static final String EX = "EX";
//
//    /**
//     * 调用set后的返回值
//     */
//    private static final String OK = "OK";

    @Override
    public boolean setNx(final String key, final String value) {
        return stringRedisTemplate.opsForValue().setIfAbsent(key, value);
    }

    @Override
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public long del(String... key) {
        return stringRedisTemplate.delete(Arrays.asList(key));
    }

    @Override
    public boolean expire(String key, long seconds) {
        return stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);
    }

    @Override
    public long incr(String key) {
        return stringRedisTemplate.opsForValue().increment(key, 1);
    }

    @Override
    public long incr(String key, long val) {
        return stringRedisTemplate.opsForValue().increment(key, val);
    }

    @Override
    public long getNum(String key) {
        String num = stringRedisTemplate.opsForValue().get(key);
        if(StringUtilExt.isEmpty(num)){
            return 0L;
        }else{
            return Long.parseLong(num);
        }
    }

    @Override
    public String ping() {
        return stringRedisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.ping();
            }
        });
    }

    @Override
    public long sadd(String key, String member) {
        return stringRedisTemplate.opsForSet().add(key, member);
    }

    @Override
    public long sremove(String key, String member) {
        return stringRedisTemplate.opsForSet().remove(key, member);
    }

    @Override
    public void set(String key, String value, long timeout, TimeUnit unit) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    @Override
    public void setIfAbsent(String key, String value) {
        stringRedisTemplate.opsForValue().setIfAbsent(key, value);
    }}

